import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Map;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;


public class TCPClient extends IoHandlerAdapter {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		SocketConnector connector = new NioSocketConnector();
		connector.setConnectTimeoutMillis(5000);
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
		connector.setHandler(new Handler());
		ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", 10001));
		connFuture.awaitUninterruptibly();
		IoSession session = connFuture.getSession();
		System.out.println("Sending jo man");
		session.write("jo man");
		Thread.sleep(10000);
	}
}

class Handler extends IoHandlerAdapter {
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		super.messageReceived(session, message);
		System.out.println("##### " + message + " ######");
	}
	
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
		System.out.println("Remote host has closed connection");
		
	}
}